package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.gulimall.auth.util.HttpUtils;

import com.atguigu.gulimall.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

import static com.atguigu.common.constant.AuthServerConstant.LOGIN_USER;

/**
 * 2022/4/15 16:11 星期五
 * OAuth2Controller  处理社交登录
 * Created by ASIA 乔帅阳 .
 * --
 * --
 */
@Slf4j
@Controller
public class OAuth2Controller {

    @Autowired
    private MemberFeignService memberFeignService;

    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session, HttpServletResponse servletResponse) throws Exception {
        //1、根据code换取AccessToken;
        Map<String,String> header = new HashMap<>();
        Map<String,String> map  = new HashMap<>();
        map.put("client_id","2977826459");
        map.put("client_secret","afbbf82f03370536a0985d5fdc131982");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.gulimall.com:82/oauth2.0/weibo/success");
        map.put("code",code);

        HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", header, map, new HashMap<>());
        //2、登录成功跳回首页
         if(response.getStatusLine().getStatusCode() == 200){
             //获取到了Token
             String json = EntityUtils.toString(response.getEntity()); //将对象转为json字符串
             SocialUser socialUser = JSON.parseObject(json, SocialUser.class);//json字符串转对象


             R oauthlogin = memberFeignService.oauthlogin(socialUser);
             if(oauthlogin.getCode() == 0){
                 MemberRespVo data = oauthlogin.getData("data", new TypeReference<MemberRespVo>() {
                 });
                 log.info("登录成功：用户: {}" ,data.toString());
                 //1、第一次使用session，命令浏览器保存卡号，JSESSIONID这个cookie
                 //以后浏览器访问哪个网站就会带上这个网站的cookie
                 //TODO 1、默认发的令牌。当前域（解决子域session共享问题）
                 //TODO 2、使用JSON的序列化方式来序列化对象到Redis中

                 session.setAttribute(LOGIN_USER,data);
                 //2、成功后跳回首页额
                 return "redirect:http://qsy.sale:82";

             }else{
                 return "redirect:http://auth.qsy.sale.com:82/login.html";
             }
         }else{
             return "redirect:http://auth.qsy.sale:82/login.html";
         }


    }
}
